package com.woniu.contorller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.form.AddAddressForm;
import com.woniu.form.ModifyUserAddressForm;
import com.woniu.model.Result;
import com.woniu.model.UserAddrees;
import com.woniu.params.AddAddressParam;
import com.woniu.params.ModifyUserAddressParam;
import com.woniu.server.TUserService;
import com.woniu.server.UserAddreesService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.websocket.server.PathParam;

/**
 * <p>
 *  用户地址信息控制层
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@Validated
@RequestMapping("/userAddrees")
@Api(tags = "用户地址信息接口")
public class UserAddreesController {
    @Autowired
    private UserAddreesService userAddreesService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 用户新增地址
     * @param form 用户新增地址所需数据
     * @param token
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @ApiOperation("新增地址")
    public Result<UserAddrees> add(@RequestBody @Validated AddAddressForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("id").toString());
        AddAddressParam param = BeanUtil.toBean(form, AddAddressParam.class);
        param.setUserId(userId);
        UserAddrees userAddrees = userAddreesService.add(param);
        return Result.newInstance(UserAddrees.class).setMessage("新增地址成功").setData(userAddrees);
    }

    /**
     * 删除我的地址信息
     * @param addressId 地址表id
     * @param token
     * @return
     * @throws Exception
     */
    @GetMapping("/del/{addressId}")
    @ApiOperation("删除地址信息")
    public Result<UserAddrees> del(@ApiParam(name = "addressId",value = "地址id")
                                       @NotNull(message = "地址id不能为空")
                                       @Positive(message = "地址id必须是正整数")
                                       @RequestParam("addressId")
                                       Integer addressId, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("id").toString());
        UserAddrees userAddrees = userAddreesService.del(addressId,userId);
        return Result.newInstance(UserAddrees.class).setMessage("删除地址信息成功").setData(userAddrees);
    }
    @PostMapping("/modify/address")
    @ApiOperation("修改地址信息")
    public Result<UserAddrees> modify(@RequestBody @Validated ModifyUserAddressForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("id").toString());
        ModifyUserAddressParam param = BeanUtil.toBean(form, ModifyUserAddressParam.class);
        param.setUserId(userId);
        UserAddrees userAddrees = userAddreesService.modify(param);
        return Result.newInstance(UserAddrees.class).setMessage("修改地址信息成功").setData(userAddrees);
    }

    /**
     * 分页查询地址信息
     * @param pageNum
     * @param pageSize
     * @param token
     * @return
     */
    @GetMapping("/selectPage")
    @ApiOperation("分页查询地址")
    public Result<Page<UserAddrees>> selectPage(@ApiParam(name = "pageNum",value = "第几页")
                                                @RequestParam(value = "pageNum",required = true,defaultValue = "1")
                                                Integer pageNum,
                                                @ApiParam(name = "pageSize",value = "每页记录数")
                                                @RequestParam(value = "pageSize",required = true,defaultValue = "10")
                                                Integer pageSize,
                                                @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("id").toString());
        Page<UserAddrees> page =  userAddreesService.selectPage(pageNum,pageSize,userId);
        return Result.newPageInstance(UserAddrees.class).setData(page).setMessage("查询地址信息成功");
    }

    /**
     * 设置默认地址
     * @param addressId 地址id
     * @param token 用户token
     * @return
     * @throws Exception
     */
    @GetMapping("/settingDefaultAddress")
    @ApiOperation("设置默认地址")
    public Result<UserAddrees> settingDefaultAddress(@ApiParam(name = "addressId",value = "地址id")
                                                         @NotNull(message = "地址id不能为空")
                                                         @Positive(message = "地址id必须是正整数")
                                                         @RequestParam("addressId")
                                                         Integer addressId,
                                                     @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("id").toString());
        UserAddrees userAddrees =  userAddreesService.settingDefaultAddress(addressId,userId);
        return Result.newInstance(UserAddrees.class).setData(userAddrees).setMessage("设置默认地址成功");
    }

    /**
     * 根据id查询地址信息
     * @param addressId 地址id
     * @return
     */
    @GetMapping("/addressById/{addressId}")
    @ApiOperation("根据id查询地址信息")
    public Result<UserAddrees> addressById(@ApiParam(name = "addressId",value = "地址id")
                                               @PathVariable("addressId")
                                               @NotNull(message = "地址id不能为空")
                                               Long addressId){
        UserAddrees addrees = userAddreesService.getById(addressId);
        if (addrees==null) {
            return Result.newInstance(UserAddrees.class).setCode(500).setMessage("不存在该地址信息");
        }
        return Result.newInstance(UserAddrees.class).setData(addrees).setMessage("查询地址信息成功");
    }

    /**
     * 根据用户id查询用户的默认地址信息
     * @param token 用户token
     * @return
     */
    @GetMapping("/addressByUserId")
    @ApiOperation("根据用户id查询用户的默认地址信息")
    public Result<UserAddrees> addressByUserId(@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("id").toString());
        UserAddrees address = userAddreesService.getOne(new QueryWrapper<UserAddrees>().eq("default_address", 1).eq("id",userId));
        if (address==null) {
            throw new Exception("还未设置默认地址");
        }
        return Result.newInstance(UserAddrees.class).setData(address).setMessage("根据用户id查询默认地址成功");
    }

    /**
     * 根据用户id查询用户的默认地址信息
     * @param userId 用户id
     * @return
     */
    @GetMapping("/addressByUserId.api/{userId}")
    @ApiOperation("根据用户id查询用户的默认地址信息")
    public Result<UserAddrees> addressByUserId(@PathVariable("userId") Integer userId) throws Exception {
        UserAddrees address = userAddreesService.getOne(new QueryWrapper<UserAddrees>().eq("default_address", 1).eq("user_id",userId));
        if (address==null) {
            return Result.newInstance(UserAddrees.class).setCode(500).setMessage("还未设置默认地址");
        }
        return Result.newInstance(UserAddrees.class).setData(address).setMessage("根据用户id查询默认地址成功");
    }
}

